package cn.school.controller.workflow;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.workflow.service.LeaveApplyService;
import cn.school.workflow.vo.LeaveApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 请假申请
 */
@RestController
@RequestMapping("/leaveApply")
public class LeaveApplyController {

    @Autowired
    private LeaveApplyService leaveApplyService;
    /**
     * 查询流程部署信息
     * @return
     * @throws ApiException
     */
    @PostMapping("/findLeaveApplyPageList")
    public CommonResult<CommonPage<LeaveApplyVO>> findLeaveApplyPageList(@RequestBody LeaveApplyVO vo) throws ApiException{
        IPage<LeaveApplyVO> pageList =  leaveApplyService.findLeaveApplyPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/cancelLeaveApply")
    @PreAuthorize("@ss.hasPermi('leave:apply:cancel')")
    public CommonResult<CommonPage<LeaveApplyVO>> cancelLeaveApply(@RequestBody WorkFlowVO  vo) throws ApiException{
        boolean flag =  leaveApplyService.cancelLeaveApply(vo);
        if(flag){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程取消失败");
    }

    @PostMapping("/createLeaveApply")
    @PreAuthorize("@ss.hasPermi('leave:apply:create')")
    public CommonResult<Integer> createLeaveApply(@RequestBody LeaveApplyVO  vo) throws ApiException{
        int flag =  leaveApplyService.createLeaveApply(vo);
        if(flag>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程创建失败");
    }

    @GetMapping("/findLeaveApplyDetail/{processInstanceId}")
    public CommonResult<LeaveApplyVO> findLeaveApplyDetail(@PathVariable("processInstanceId") Long processInstanceId) throws ApiException{
        LeaveApplyVO leaveApplyVO =  leaveApplyService.findLeaveApplyDetail(processInstanceId);
        if(leaveApplyVO!=null){
            return CommonResult.success(leaveApplyVO);
        }
        return  CommonResult.failed("查询不单申请单");
    }


}
